package com.baowenli.server;


import com.baowenli.mapper.CheckInMapper;
import com.baowenli.mapper.OrderInfoMapper;
import com.baowenli.pojo.CheckIn;
import com.baowenli.pojo.Order;
import com.baowenli.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;
import java.util.function.Consumer;

public class OrderInfoService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 生成订单
     * @return
     */
    public int insertOrderInfo(int orderId,int orderTypeId,String orderTyId,int userId,String name,String phone
            ,int roomId,double orderCost,int workId,int roomInfoId) {
        //2.获取sqlSession
        SqlSession sqlSession = factory.openSession();
        //3.获取BrandMapper
        OrderInfoMapper mapper = sqlSession.getMapper(OrderInfoMapper.class);
        //4.调用方法
        int temp = mapper.insertOrderInfo(orderId, orderTypeId, orderTyId, userId, name, phone, roomId, orderCost,
                workId,roomInfoId);
        sqlSession.commit();
        sqlSession.close();
        return temp;
    }

    /**
     *  进行算出住的天数的总共价钱
     * */
    public Order findHomePrice(int roomNumber,int orderID){
        //2.获取sqlSession
        SqlSession sqlSession = factory.openSession();
        //3.获取BrandMapper
        OrderInfoMapper mapper = sqlSession.getMapper(OrderInfoMapper.class);
        //4.调用方法
        Order orderInfo = mapper.findCountDayPrice(roomNumber,orderID);
        sqlSession.commit();
        sqlSession.close();
        return orderInfo;
    }

    /**
     *  进行算出特定日期之后的订单信息
     * */
    public List<Order> selectAllOrderInfoAfter(String datetime){
        //2.获取sqlSession
        SqlSession sqlSession = factory.openSession();
        //3.获取BrandMapper
        OrderInfoMapper mapper = sqlSession.getMapper(OrderInfoMapper.class);

        //4.调用方法
        List<Order> orders = mapper.selectAllOrderInfoAfter(datetime);
        orders.forEach(new Consumer<Order>() {
            @Override
            public void accept(Order order) {
                System.out.println("\t\t顾客的姓名："+order.getName()+""+"\t顾客的预定方式是："+order.getOrderType()+"\t顾客的房号是："+order.getRoomInfoId()+
                        "\t顾客支付的价钱是："+order.getOrderCost());
            }
        });
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        sqlSession.commit();
        sqlSession.close();
        return orders;
    }

    /**
     *  进行算出特定日期之后的订单信息
     * */
    public List<Order> selectAllOrderInfoBefore(String datetime){
        //2.获取sqlSession
        SqlSession sqlSession = factory.openSession();
        //3.获取BrandMapper
        OrderInfoMapper mapper = sqlSession.getMapper(OrderInfoMapper.class);

        //4.调用方法
        List<Order> orders = mapper.selectAllOrderInfoBefore(datetime);
        orders.forEach(new Consumer<Order>() {
            @Override
            public void accept(Order order) {
                System.out.println("\t\t顾客的姓名："+order.getName()+""+"\t顾客的预定方式是："+order.getOrderType()+"\t顾客的房号是："+order.getRoomInfoId()+
                        "\t顾客支付的价钱是："+order.getOrderCost());
            }
        });
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        sqlSession.commit();
        sqlSession.close();
        return orders;
    }


    /**
     *  进行算出特定日期之后的订单信息
     * */
    public List<Order> selectAllOrderInfoNow(String datetime){
        //2.获取sqlSession
        SqlSession sqlSession = factory.openSession();
        //3.获取BrandMapper
        OrderInfoMapper mapper = sqlSession.getMapper(OrderInfoMapper.class);

        //4.调用方法
        List<Order> orders = mapper.selectAllOrderInfoNow(datetime);
        orders.forEach(new Consumer<Order>() {
            @Override
            public void accept(Order order) {
                System.out.println("\t\t顾客的姓名："+order.getName()+""+"\t顾客的预定方式是："+order.getOrderType()+"\t顾客的房号是："+order.getRoomInfoId()+
                        "\t顾客支付的价钱是："+order.getOrderCost());
            }
        });
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        sqlSession.commit();
        sqlSession.close();
        return orders;
    }
}
